//
//  TRWebViewController.swift
//  ThinkerRide
//
//  Created by sqkj on 2021/6/3.
//  Copyright © 2021 Chongqing Thinkerride Technology Co., Ltd. All rights reserved.
//

import UIKit
import WebKit
import SnapKit

public class TRWebViewController: UIViewController {

    //浏览器内核
    lazy var webContainer:TRWebViewContainer = {
        let webContainer = TRWebViewContainer()
        return webContainer
    }()
    
    public lazy var closeButton:UIButton = {
        let closeButton = UIButton.init(type: UIButton.ButtonType.custom)
        closeButton.frame = CGRect.init(x: 0, y: 0, width: 37, height: 30)
        closeButton .setTitle("关闭", for: UIControl.State.normal)
        closeButton.isHidden = true
        closeButton.setTitleColor(UIColor.red, for: UIControl.State.normal)
        closeButton.addTarget(self, action: #selector(closeButtonClick), for: UIControl.Event.touchUpInside)
        return closeButton
    }()
    // 返回按钮
    public lazy var backButton:UIButton = {
         let backButton = UIButton.init(type: UIButton.ButtonType.custom)
        backButton.contentHorizontalAlignment = .left
        backButton.frame = CGRect.init(x: 0, y: 0, width: 60, height:50)
         backButton.addTarget(self, action: #selector(backButtonClick), for: UIControl.Event.touchUpInside)
        return backButton
    }()
    
    /// 网址
    var url:URL?
    /// 网址字符串
    var urlstr:String?
    /// 本地网页名称
    var fileName:String?

    public var webViewStartLoadBlock:((WKWebView)->Void)?
    
    public var webViewDidLoadBlock:((WKWebView)->Void)?
    
    public var webCloseBlock:(()->Void)?
        
    public override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = ThemeColor
        
        webContainer.webViewStartLoadBlock = {[weak self] webView in
            self?.webViewStartLoadBlock?(webView)
        }
        
        webContainer.webViewFinishLoadBlock = {[weak self] webView in
            self?.webViewDidLoadBlock?(webView)
        }
        
        
    }
    
    init() {
        super.init(nibName: nil, bundle: nil)
        self.makeInit()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func makeInit(){
 
        self.bindViewModel()
        self.setupViews()
        self.updateLayoutConstraints()
        self.layoutNavigation()
    }

    func bindViewModel (){
        self.view.backgroundColor = ThemeColor
        self.edgesForExtendedLayout = UIRectEdge.bottom;
    }
    
    func updateLayoutConstraints(){
        self.webContainer.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view);
        }
    }
    
    func setupViews(){
        self.view.addSubview(self.webContainer)
    }
    
    func layoutNavigation(){
        let backItem = UIBarButtonItem.init(customView: self.backButton)
        self.navigationItem.leftBarButtonItems = [backItem]
    }
    
    // 加载网页字符串地址
    func initWithURLStr(str:String,title:String = "")-> TRWebViewController{
        let webVC = TRWebViewController()
        webVC.webContainer.loadURLstr(url: str)
        webVC.navigationItem.title = title
        webVC.webContainer.titleDidChangeBlock = { titleTmp in
            webVC.navigationItem.title = titleTmp
        }
        return webVC
    }
    
    // 加载网页网址
    func initWithURL(url:URL,title:String = "")-> TRWebViewController{
        let webVC = TRWebViewController()
        webVC.webContainer.loadURL(url: url)
        webVC.navigationItem.title = title
        webVC.webContainer.titleDidChangeBlock = { titleTmp in
            webVC.navigationItem.title = titleTmp
        }
        return webVC
    }
    
    // 加载本地网页
    func initWithFileName(fileName:String,title:String = "")-> TRWebViewController{
        let webVC = TRWebViewController()
        webVC.webContainer.loadLocalHtmlFile(name: fileName)
        webVC.navigationItem.title = title
        webVC.webContainer.titleDidChangeBlock = { titleTmp in
            webVC.navigationItem.title = titleTmp
        }
        return webVC
    }
    
    // 关闭
    @objc func closeButtonClick(){
        self.navigationController?.dismiss(animated: true, completion: {
            self.webCloseBlock?()
        })
    }
    
    // 返回
    @objc func backButtonClick(){
        if self.webContainer.webView.canGoBack {
            self.webContainer.webView.goBack()
        }else{
            if self.navigationController?.viewControllers.count ?? 0 > 1{
                self.navigationController?.popViewController(animated: true)
            }else{
                self.navigationController?.dismiss(animated: true, completion: {
                    self.webCloseBlock?()
                })
            }
        }
    }

}
